package calender;

import org.junit.Test;
import utils.DateUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author: hujian
 * @Date: 2019/9/19 20:13
 * @Description: <描述>
 */
public class TestCalender {

    public static void main(String[] args) {
        Calendar c = Calendar.getInstance();
        Date date = DateUtils.parse("2019-11-05 20:42:23");
        c.setTime(date);
        System.out.println(DateUtils.formatDate(c.getTime()));
        c.set(Calendar.WEEK_OF_YEAR, c.get(Calendar.WEEK_OF_YEAR) - 1);
        System.out.println(DateUtils.formatDate(c.getTime()));
    }

    @Test
    public void testa(){
        String s = new SimpleDateFormat("yyyy-MM-dd")
                .format(getLastDay(2017, 9));
        System.out.println(s);

    }
    private static Date getLastDay(int year, int month) {
        //获取Calendar类的实例
        Calendar c = Calendar.getInstance();
        //设置年份
        c.set(Calendar.YEAR, year);
        //设置月份，因为月份从0开始，所以用month - 1
        c.set(Calendar.MONTH, month - 1);
        //获取当前时间下，该月的最大日期的数字
        int lastDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        //将获取的最大日期数设置为Calendar实例的日期数
        c.set(Calendar.DAY_OF_MONTH, lastDay);
        return c.getTime();
    }

}
